/*
 * CInstrumentMarketData.h
 *
 *  Created on: 2011-11-18
 *      Author: Owner
 */

#ifndef CINSTRUMENTMARKETDATA_H_
#define CINSTRUMENTMARKETDATA_H_

#define MAX_DATA 240

#include "ThostFtdcMdApi.h"
#include "InstrumentMarketData.h"
#include "string.h"
#include "Lock.h"
#include "qtapi.h"
#include "mystruct.h"

class CInstrumentMarketData {
private:
	MarketDataField  m_MarketData[MAX_DATA];
	int m_nIndex;

public:
	CInstrumentMarketData(){
		m_nIndex = 0;
	}

	virtual ~CInstrumentMarketData(){

	}

	void Init(CThostFtdcDepthMarketDataField *pInitial){
		for(int i=0;i<MAX_DATA;i++){
			memset(&m_MarketData[i],0,sizeof(MarketDataField));
			MarketDataField *pOld  = &m_MarketData[i];
			strcpy(pOld->ExchangeID,"CFFEX");

			strcpy(pOld->TradingDay,pInitial->TradingDay);
			strcpy(pOld->InstrumentID,pInitial->InstrumentID);
			pOld->UpperLimitPrice = pInitial->UpperLimitPrice;
			pOld->LowerLimitPrice = pInitial->LowerLimitPrice;
			pOld->PreClosePrice = pInitial->PreClosePrice;
			pOld->PreOpenInterest = pInitial->PreOpenInterest;
			pOld->PreSettlementPrice = pInitial->PreSettlementPrice;
		}
	}

	inline MarketDataField * AddMarketData(CThostFtdcDepthMarketDataField *pField){
		MarketDataField *pData = NULL;

		pData = &m_MarketData[m_nIndex];

		if(!CopyMD(pData,pField)){
			return NULL;
		}

		m_nIndex++;
		if(m_nIndex >= MAX_DATA){
			m_nIndex = 0;
		}

		return pData;
	}

private:
	inline bool CopyMD(MarketDataField *pData,CThostFtdcDepthMarketDataField * pField){
		pData->OpenPrice = pField->OpenPrice;
		pData->LastPrice = pField->LastPrice;

		pData->BidPrice[0] = pField->BidPrice1;
		pData->AskPrice[0] = pField->AskPrice1;
		pData->BidVolume[0] = pField->BidVolume1;
		pData->AskVolume[0] = pField->AskVolume1;

		pData->BidPrice[1] = pField->BidPrice2;
		pData->AskPrice[1] = pField->AskPrice2;
		pData->BidVolume[1] = pField->BidVolume2;
		pData->AskVolume[1] = pField->AskVolume2;

		pData->BidPrice[2] = pField->BidPrice3;
		pData->AskPrice[2] = pField->AskPrice3;
		pData->BidVolume[2] = pField->BidVolume3;
		pData->AskVolume[2] = pField->AskVolume3;

		pData->BidPrice[3] = pField->BidPrice4;
		pData->AskPrice[3] = pField->AskPrice4;
		pData->BidVolume[3] = pField->BidVolume4;
		pData->AskVolume[3] = pField->AskVolume4;

		pData->BidPrice[4] = pField->BidPrice5;
		pData->AskPrice[4] = pField->AskPrice5;
		pData->BidVolume[4] = pField->BidVolume5;
		pData->AskVolume[4] = pField->AskVolume5;

		pData->Ammount = pField->Turnover;
		pData->OpenInterest = pField->OpenInterest;
		pData->Volume = pField->Volume;
		pData->HighestPrice = pField->HighestPrice;
		pData->LowestPrice = pField->LowestPrice;
		//pData->SettlementPrice = pField->SettlementPrice;
		strcpy(pData->UpdateTime,pField->UpdateTime);

		//added 2013-12-20
		if(pData->UpdateTime[0]=='2'){
			pData->UpdateTime[0]='0';
			if(pData->UpdateTime[1]>='1'){
				pData->UpdateTime[1]--;
			}else{
				return false;
			}
		}else if(pData->UpdateTime[0]=='0' && pData->UpdateTime[1]<='3'){
			pData->UpdateTime[1]+=3;
		}

		pData->MilliSeconds = pField->UpdateMillisec;
		pData->AveragePrice = pField->SettlementPrice;
		return true;
	}
};

#endif /* CINSTRUMENTMARKETDATA_H_ */
